package com.alm.token.ui.viewpagefragment;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alm.token.ConStant;
import com.alm.token.GlideApp;
import com.alm.token.MyApp;
import com.alm.token.R;
import com.alm.token.base.BaseAdapter;
import com.alm.token.base.BaseFragment;
import com.alm.token.common.RefreshLoadMoreManager;
import com.alm.token.ui.activity.BillActivity;
import com.alm.token.ui.activity.BlockWalletLoginActivity;
import com.alm.token.ui.activity.TransferQrCodeActivity_new;
import com.alm.token.ui.activity.TurnOutBiTB_FukuanActivity;
import com.alm.token.ui.data.BaseDialogBean;
import com.alm.token.ui.data.bean.CurrencyChangeData;
import com.alm.token.ui.data.bean.CurrencyChangeIndexData;
import com.alm.token.ui.data.bean.CurrencyChangeListData;
import com.alm.token.ui.data.bean.DrawIndexData;
import com.alm.token.ui.data.bean.MiningWalletTypeIndexData;
import com.alm.token.ui.data.bean.RegisterData;
import com.alm.token.ui.data.bean.UserWalletData;
import com.alm.token.ui.fragmentDialog.CandyDialogFragment;
import com.alm.token.ui.persenter.MyPreenter;
import com.alm.token.ui.view.CurrencyChangeView;
import com.alm.token.utils.CommonUtil;
import com.alm.token.utils.LogUtil;
import com.alm.token.widget.CustomLinearLayoutManager;
import com.alm.token.widget.HomeGridLayout1;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.chad.library.adapter.base.BaseViewHolder;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.yzq.zxinglibrary.android.CaptureActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.web3j.crypto.CipherException;
import org.web3j.crypto.Keys;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import baibeiwallet.BaibeiWallet;
import baibeiwallet.Constant;
import baibeiwallet.WalletDaoUtils;
import baibeiwallet.WalletManager;
import baibeiwallet.WalletManagerActivity1;
import baibeiwallet.activity.TokenDetailActivity1;
import baibeiwallet.activity.TransferActivity1;
import baibeiwallet.event.RefreshPriceEvent;
import baibeiwallet.event.RefreshWalletEvent;
import baibeiwallet.event.SelWalletEvent;
import baibeiwallet.model.ETHWallet3;
import baibeiwallet.model.TokenModel;
import baibeiwallet.solidity.Token;
import baibeiwallet.utils.BaiBeiWalletUtils;
import baibeiwallet.utils.BaibeiTransferUtils;
import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.SingleObserver;
import io.reactivex.disposables.Disposable;

//import com.alm.token.ui.activity.QRCodeScannerActivity;
//import com.yzq.zxinglibrary.android.CaptureActivity;
//import com.uuzuche.lib_zxing.activity.CaptureActivity;
//import com.uuzuche.lib_zxing.activity.CodeUtils;
//import com.uuzuche.lib_zxing.activity.ZXingLibrary;

/**
 * Created by en on 2019/1/4.
 */

public class BannerTwoFragment extends BaseFragment implements CurrencyChangeView<Object>,HomeGridLayout1.BtnInterface {
    @BindView(R.id.tv_sysmsg)
    TextView tvSysmsg;
    @BindView(R.id.img_morebizhong)
    ImageView imgMorebizhong;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.wdgt_homegridly)
    HomeGridLayout1 wdgtHomegridly;

    View rootView;
    @BindView(R.id.nodatashow_body)
    RelativeLayout nodatashowBody;

    private RefreshLoadMoreManager<UserWalletData.DataBean.WalletsBean> manager;
    private List<UserWalletData.DataBean.WalletsBean> wallets = new ArrayList<>();
    private List<UserWalletData.DataBean.SysMsgBean> sysMsg;
    private UserWalletData.DataBean dataBean;
    private static final int QRCODE_SCANNER_REQUEST = 1100;

    public BannerTwoFragment() {

    }

    public static BannerTwoFragment newInstance(UserWalletData.DataBean dataBean) {
        BannerTwoFragment fragment = new BannerTwoFragment();
        Bundle args = new Bundle();
        args.putParcelable("dataBean", dataBean);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected MyPreenter initPreenter() {
        return new MyPreenter(this);
    }

    @Override
    protected int getLayoutId() {
        rootView = LayoutInflater.from(getContext()).inflate(R.layout.item_banner_2, null);
        return R.layout.item_banner_2;
    }

    @Override
    protected void initVariables() {

    }

    @Override
    protected void initViews(View view) {
        initManager();
        wdgtHomegridly.setBtnInterface(this);
        recyclerView.setVisibility(View.GONE);
        nodatashowBody.setVisibility(View.VISIBLE);

    }


    BaibeiWallet mBaibeiWallet;
    String baibeiWalletAdress = "";
    Token mContract;

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onRefreshWalletEvent(RefreshWalletEvent sel) {
        LogUtil.d("刷新选择的钱包1");
        showWaitDialog();
        ETHWallet3 ethWallet = WalletDaoUtils.getCurrent();
        if (ethWallet != null) {
            try {
                LogUtil.d("刷新选择的钱包2");
                mBaibeiWallet = BaiBeiWalletUtils.loadByKeyStore(WalletDaoUtils.getCurrent().getPassword(), WalletDaoUtils.getCurrent().getKeystorePath());
                mBaibeiWallet.setPassword(WalletDaoUtils.getCurrent().getPassword());
                mBaibeiWallet.setWalletName(WalletDaoUtils.getCurrent().getName());
                baibeiWalletAdress = mBaibeiWallet.getWalletFile().getAddress();

                if (mBaibeiWallet == null) {
                    showToast(getString(R.string.profile_hint_item6));
//                    Toast.makeText(getActivity(), "钱包为空", Toast.LENGTH_SHORT).show();
                } else {
                    getBalanceForETH(baibeiWalletAdress);
                }


                loadContract(Constant.G12ContractAddress, Constant.G12ContractName);


            } catch (IOException e) {
                e.printStackTrace();
            } catch (CipherException e) {
                e.printStackTrace();
            }
        } else {
            mBaibeiWallet = null;
            wallets.clear();
            manager.setData(wallets);
            hideWaitDialog();
        }


    }

    int mIndex = -1;

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onSelWalletEvent1(SelWalletEvent sel) {
//        mBaibeiWallet = WalletManager.getInstance().getWallet(sel.index);
        LogUtil.d("刷新选择钱包位置1");
        if (sel.index >= 0) {
            mIndex = sel.index;
        }
        if (sel.isiInit) {//初始化
            hideWaitDialog();
        }

    }

    private void getBalanceForETH(String addr) {
        BaibeiTransferUtils.getBalanceForETH(addr)
                .subscribe(new SingleObserver<BigInteger>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onSuccess(BigInteger bigInteger) {
                        BigDecimal bigDecimal = new BigDecimal(bigInteger + ".0000");
                        BigDecimal bigDecimal1 = new BigDecimal("1000000000000000000.0000");
                        LogUtil.d("余额：" + bigDecimal.divide(bigDecimal1, BigDecimal.ROUND_HALF_UP));
//                        Toast.makeText(getActivity(), "ETH余额：" + bigDecimal.divide(bigDecimal1, BigDecimal.ROUND_HALF_UP), Toast.LENGTH_SHORT).show();

//                        if(wallets.size()==2)

                        UserWalletData.DataBean.WalletsBean walletsBean = new UserWalletData.DataBean.WalletsBean();
                        walletsBean.setName("ETH");
                        walletsBean.setMoney(Double.parseDouble(bigDecimal.divide(bigDecimal1, BigDecimal.ROUND_HALF_UP).toString()));
                        if (wallets != null && wallets.size() == 2) {
                            wallets.set(0, walletsBean);
                        } else {
                            wallets.add(0, walletsBean);
                        }


                        if (wallets != null && wallets.size() != 0) {
                            for (int i = 0; i < wallets.size(); i++) {
                                wallets.get(i).setLayoutId(HomeWalletAdapter.LAYOUT_ID);
                            }
                            manager.setData(wallets);
                        }

                        if (wallets != null && wallets.size() != 0) {
                            recyclerView.setVisibility(View.VISIBLE);
                            nodatashowBody.setVisibility(View.GONE);

                        } else {
                            recyclerView.setVisibility(View.GONE);
                            nodatashowBody.setVisibility(View.VISIBLE);
                        }


//                        mTvBalanceEth.setText("余额：" + bigDecimal.divide(bigDecimal1, BigDecimal.ROUND_HALF_UP));
//                        mTvBalanceEth.setText(bigInteger.toString() + "");
                    }

                    @Override
                    public void onError(Throwable e) {
                        LogUtil.d("余额查询有误！" + e.getMessage());
//                        mTvBalanceEth.setText("余额查询有误！" + e.getMessage());
                    }
                });
    }

    private void loadContract(String contractAddr, String tokenName) {
        TokenModel mTokenModel = new TokenModel(contractAddr, tokenName, false);
        if (mTokenModel != null && mTokenModel.isEth) {
            //以太坊不需要加载合约
            return;
        }
        BaibeiTransferUtils.loadContract(mBaibeiWallet, mTokenModel.getAddr(), getGasPrice(), getGasLimit())
                .subscribe(new SingleObserver<Token>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onSuccess(Token token) {
//                        Toast.makeText(getActivity(), "合约加载完成！", Toast.LENGTH_SHORT).show();
                        mContract = token;
                        getBalance(tokenName);
                    }

                    @Override
                    public void onError(Throwable e) {
//                        showToast("合约加载失败!");
//                        Toast.makeText(getActivity(), "合约加载失败！", Toast.LENGTH_SHORT).show();
                    }
                });
    }

    private void getBalance(String tokenName) {
        if (mContract == null) {
            Toast.makeText(getActivity(), getString(R.string.profile_hint_item7), Toast.LENGTH_SHORT).show();
            return;
        }

        BaibeiTransferUtils.balanceOf(mContract, mBaibeiWallet.getWalletFile().getAddress())
                .subscribe(mBalanceObserver);

    }


    private SingleObserver<BigInteger> mBalanceObserver = new SingleObserver<BigInteger>() {
        @Override
        public void onSubscribe(Disposable d) {
        }

        @Override
        public void onSuccess(BigInteger bigInteger) {
//            Toast.makeText(getActivity(), "代币余额：" + bigInteger.toString(), Toast.LENGTH_SHORT).show();

            LogUtil.d("代币余额：" + bigInteger.toString());
            UserWalletData.DataBean.WalletsBean walletsBean = new UserWalletData.DataBean.WalletsBean();
            walletsBean.setName(Constant.G12ContractName);
            walletsBean.setMoney(Double.parseDouble(bigInteger.toString()));

            if (wallets != null && wallets.size() == 2) {
                wallets.set(1, walletsBean);
            } else {
                wallets.add(walletsBean);
            }


            if (wallets != null && wallets.size() != 0) {
                for (int i = 0; i < wallets.size(); i++) {
                    wallets.get(i).setLayoutId(HomeWalletAdapter.LAYOUT_ID);
                }
                manager.setData(wallets);
            }

            if (wallets != null && wallets.size() != 0) {
                recyclerView.setVisibility(View.VISIBLE);
                nodatashowBody.setVisibility(View.GONE);

            } else {
                recyclerView.setVisibility(View.GONE);
                nodatashowBody.setVisibility(View.VISIBLE);
            }
//            mTvBalanceToken.setText(bigInteger.toString());
        }

        @Override
        public void onError(Throwable e) {
            LogUtil.d("代币余额查询有误！" + e.getMessage());
//            mTvBalanceToken.setText("余额查询有误！" + e.getMessage());
        }
    };

    private BigInteger getGasPrice() {
        return BigInteger.valueOf(1000);
    }

    private BigInteger getGasLimit() {
        return BigInteger.valueOf(600000);
    }


    @Override
    public void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    TokenModel mTokenModel;
    public String candyUrl = "";
    public String code = "0";

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == QRCODE_SCANNER_REQUEST && data != null && data.getExtras() != null) {

//            String result = null;//data.getStringExtra(com.yzq.zxinglibrary.common.Constant.CODED_CONTENT);

            String result = data.getStringExtra(com.yzq.zxinglibrary.common.Constant.CODED_CONTENT);
            Log.i("TAG", "扫描结果:" + result);
            if (!TextUtils.isEmpty(result)) {
                //https://api.coom.pub/api/candy/getCandy?mycode=104
                //说明是糖果地址,需要请求接口,领取糖果   /api/candy/getCandy?mycode=104
                if (result.indexOf("https://") != -1 || result.indexOf("http://") != -1) {
                    String[] sstrs = result.split("/api/");
                    if (sstrs.length >= 2) {
                        candyUrl = sstrs[1];
                        LogUtil.e("candyUrl=" + candyUrl);
                        String[] sstr2 = candyUrl.split("\\?");
                        if(sstr2.length>=2){
                            String[] sstr3 = candyUrl.split("=");
                            candyUrl=sstr2[0];
                            if(sstr3.length>=2)
                                code=sstr3[1];
                        }
                        preenter.getDrawCandyData(ConStant.HOST + "/api/" + candyUrl,code);
                    }
                }else if (result.indexOf("CollectionPayment") != -1) {//{"coinId":"2","type":"CollectionPayment","price":"1","address":"algor","coinName":"MIO"}
                    //说明是比币互转付款二维码扫码
                    Intent intent = new Intent(getContext(), TurnOutBiTB_FukuanActivity.class);
                    intent.putExtra("scanResult", result + "");
                    startActivity(intent);
                } else {
                    mTokenModel = new TokenModel("", "ETH", true);
                    Intent i = new Intent(getActivity(), TransferActivity1.class);
                    i.putExtra("index", mIndex);
                    i.putExtra("token", mTokenModel);
                    i.putExtra("adress", result);
                    startActivity(i);
                }
            } else {
                Toast.makeText(getActivity(), getString(R.string.profile_hint_item3), Toast.LENGTH_SHORT).show();
            }
        } else {
//            Toast.makeText(getActivity(), getString(R.string.profile_hint_item4), Toast.LENGTH_SHORT).show();
        }


    }

    private void initManager() {
        HomeWalletAdapter adapter = new HomeWalletAdapter(getActivity());
        manager = new RefreshLoadMoreManager.Builder(getActivity())
                .setAdapter(adapter)
                .setRecyclerView(recyclerView)
                .setLayoutManager(new CustomLinearLayoutManager(getActivity()))
                .build();

//        recyclerView.setNestedScrollingEnabled(false);

    }

    List<ETHWallet3> ethWallets = new ArrayList<>();
    List<BaibeiWallet> mDatas = new ArrayList<>();

    @Override
    protected void loadData() {

        showWaitDialog();

        ethWallets = WalletDaoUtils.loadAll();
        new Thread(new Runnable() {
            @Override
            public void run() {
                mDatas = WalletManager.getInstance().getWalletAll();
                if (ethWallets != null && ethWallets.size() != 0) {
                    for (ETHWallet3 ethWallet : ethWallets) {
                        boolean ss = true;
                        for (BaibeiWallet baibeiWallet1 : mDatas) {
                            if (Keys.toChecksumAddress(baibeiWallet1.getWalletFile().getAddress()).equalsIgnoreCase(ethWallet.getAddress())) {
                                ss = false;
                                break;
                            }
                        }
                        if (ss) {
                            BaibeiWallet baibeiWallet = null;
                            try {
                                if (!TextUtils.isEmpty(ethWallet.getKeystorePath()) && !TextUtils.isEmpty(ethWallet.getPassword())) {
                                    baibeiWallet = BaiBeiWalletUtils.loadByKeyStore(ethWallet.getPassword(), ethWallet.getKeystorePath());
                                } else if (!TextUtils.isEmpty(ethWallet.getMnemonic()) && !TextUtils.isEmpty(ethWallet.getPassword())) {
                                    baibeiWallet = BaiBeiWalletUtils.generateBip44Wallet(ethWallet.getMnemonic(), ethWallet.getPassword());
                                }
                            } catch (CipherException e) {
                                e.printStackTrace();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            if (baibeiWallet != null) {
                                baibeiWallet.setPassword(ethWallet.getPassword());
                                baibeiWallet.setWalletName(ethWallet.getName());
                                WalletManager.getInstance().addWallet(baibeiWallet, false);
                            }
                        }
                    }
                }

                if (mDatas != null && mDatas.size() != 0) {
                    for (int i = 0; i < mDatas.size(); i++) {

                        if (WalletDaoUtils.getCurrent() != null && Keys.toChecksumAddress(mDatas.get(i).getWalletFile().getAddress())
                                .equalsIgnoreCase(WalletDaoUtils.getCurrent().getAddress())) {
//                            EventBus.getDefault().post(new SelWalletEvent(i));
                            EventBus.getDefault().post(new SelWalletEvent(i, true));
                            LogUtil.d("初始化钱包数据成功");
                            break;
                        }
                    }
                }


            }
        }).start();

        if (ethWallets == null || ethWallets.size() == 0) {
            wallets.clear();
            manager.setData(wallets);
            hideWaitDialog();
        }
    }

    @Override
    public void onClick(View v) {

    }

    @OnClick({R.id.img_morebizhong, R.id.tv_sysmsg})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_morebizhong:
                showToast(getString(R.string.profile_hint_item8));


//                Intent intent = new Intent(getContext(), MessageActivity.class);
//                intent.putExtra("type", 1);
//                startActivity(intent);
                break;
            case R.id.tv_sysmsg:
//                if (sysMsg != null && sysMsg.size() != 0) {
//                    Intent intent1 = new Intent(getContext(), MessageDetailActivity.class);
//                    intent1.putExtra("intentType", 1);
//                    intent1.putExtra("sysMsgBean", sysMsg.get(0));
//                    startActivity(intent1);
//                }

                break;

        }
    }

    public double price = 0;

    @Override
    public void getData(Object o) {

    }

    @Override
    public void jumpToLoginAct(int staue) {
        if (staue == 3) {
//            showToast(getString(R.string.common_relogin));
//            Intent intent = new Intent();
//            intent.setAction(ConStant.EXIT_APP);
//            getActivity().sendBroadcast(intent);
//            startActivity(BlockWalletLoginActivity.class);
//            getActivity().finish();
        }else if (staue == 4) {
//            showToast(getString(R.string.paypasw_hint_setpaypsw));
//            Intent intent = new Intent(getActivity(),PayPwdActivity.class);
//            intent.putExtra("PayPwdActivityType",PayPwdActivity.PAYPSW_SET);
//            startActivity(intent);
//            getActivity().finish();
            showToSetPaypswDialog();
        }else if (staue == 666) {
            showToUpdata();
        }
    }

    @Override
    public void onError(String error) {

    }

    @Override
    public void ShowProgress() {

    }

    @Override
    public void hideProgress() {

    }

    @Override
    public void getCurrencyChangeData(CurrencyChangeData currencyChangeData) {

    }

    @Override
    public void getCurrencyChangeListData(CurrencyChangeListData currencyChangeListData) {

    }

    @Override
    public void getCurrencyChangeIndexData(CurrencyChangeIndexData currencyChangeIndexData) {

    }

    @Override
    public void getDrawIndexData(DrawIndexData drawIndexData) {

    }

    @Override
    public void getDrawCandyData(RegisterData drawCandyData) {

        // {"msg":"恭喜获得zuoqi168派发0.0796G12","status":0,"data":null,"success":false,"total":null}
        if (drawCandyData.getStatus() == 0) {
            userMsgBean = new UserWalletData.DataBean.UserMsgBean();
            userMsgBean.setTitle("糖果");
            userMsgBean.setContent(drawCandyData.getMsg());
            userMsgBean.setEnTitle("Candy");
            userMsgBean.setEnContent(drawCandyData.getMsg());
            showCodeDialog();

        } else {
            showToast(drawCandyData.getMsg());
        }
    }

    @Override
    public void getMiningWalletTypeIndexData(MiningWalletTypeIndexData miningWalletTypeIndexData) {

    }

    @Override
    public void onTab(View view) {
        switch (view.getId()) {
            case R.id.tab1:
//                        showToast("扫一扫");


//                        showCamera();

                if (mBaibeiWallet != null && mIndex != -1) {

                    //扫码
                    RxPermissions rxPermissions = new RxPermissions(getActivity());
                    rxPermissions.request(Manifest.permission.CAMERA)
                            .subscribe(new Observer<Boolean>() {
                                @Override
                                public void onSubscribe(Disposable d) {
                                }

                                @Override
                                public void onNext(Boolean granted) {
                                    if (granted) {
                                        if (!CommonUtil.isCameraOpen()) {
//                                                    ZXingLibrary.initDisplayOpinion(getActivity().getApplicationContext());
////                                                    Intent intent = new Intent(getActivity(), CaptureActivity.class);
//                                                    Intent intent = new Intent(getActivity(), QRCodeScannerActivity.class);
//                                                    startActivityForResult(intent, QRCODE_SCANNER_REQUEST);

                                            Intent intent = new Intent(getActivity(), CaptureActivity.class);
                                            intent.putExtra(com.yzq.zxinglibrary.common.Constant.INTENT_ZXING_CONFIG, MyApp.getZxingConfig());
                                            startActivityForResult(intent, QRCODE_SCANNER_REQUEST);


                                        } else {
                                            showToast(getString(R.string.profile_hint_item1));
                                        }
                                    } else {
                                        Toast.makeText(getActivity(), getString(R.string.profile_hint_item2), Toast.LENGTH_SHORT).show();
                                    }
                                }

                                @Override
                                public void onError(Throwable e) {
                                }

                                @Override
                                public void onComplete() {
                                }
                            });

                } else {

                    if (WalletDaoUtils.getCurrent() == null) {
//                                preenter.getDrawIndexData("1");
                        showToast(getString(R.string.profile_hint_item5));
                    }

//                            preenter.getDrawIndexData("1");
//                            showToast("请稍后重试");
                }


                break;
            case R.id.tab2:

                if (WalletDaoUtils.getCurrent() == null) {
//                                preenter.getDrawIndexData("1");
                    showToast(getString(R.string.profile_hint_item5));
                    break;
                }
                mTokenModel = new TokenModel("", "ETH", true);
                Intent i = new Intent(getActivity(), TransferActivity1.class);
                i.putExtra("index", mIndex);
                i.putExtra("token", mTokenModel);

                i.putExtra("type", 1);
//                        i.putExtra("adress", "99");

                startActivity(i);


//                        showToast("提取");

//                        if (MyApp.getDrawIndexDataBean() != null) {
//                            startActivity(TurnOutActivity.class);
//                        } else {
//                            preenter.getDrawIndexData("1");
//                            showToast("请稍后重试");
//                        }

                break;
            case R.id.tab3:
                if (WalletDaoUtils.getCurrent() == null) {
//                                preenter.getDrawIndexData("1");
                    showToast(getString(R.string.profile_hint_item5));
                    break;
                }
                Intent intent1 = new Intent(getActivity(), TransferQrCodeActivity_new.class);
//                Intent intent1 = new Intent(getActivity(), TransferQrCodeActivity.class);
                intent1.putExtra("jiedianwallet", true);
                intent1.putExtra("bizhongType", 1);
                startActivity(intent1);
//                        showToast("充值");
//                        startActivity(TransferQrCodeActivity.class);
                break;
            case R.id.tab4:
//                        showToast("钱包管理");
                startActivity(WalletManagerActivity1.class);
//                        if (MyApp.getCurrencyChangeIndexDataBean() != null) {
//                            startActivity(TransformActivity.class);
//                        } else {
//                            preenter.getCurrencyChangeIndexData();
//                            showToast("请稍后重试");
//                        }
                break;
        }
    }

    public class HomeWalletAdapter extends BaseAdapter<UserWalletData.DataBean.WalletsBean> {

        public static final int LAYOUT_ID = R.layout.item_home_wallet;

        public HomeWalletAdapter(@NonNull Context context) {
            super(context);
        }

        @Override
        protected void convert(final BaseViewHolder helper, final UserWalletData.DataBean.WalletsBean item, int itemType) {
            if (item != null) {
                hideWaitDialog();
            }
            ImageView bg = helper.getView(R.id.bg);
//            if (item.getIndex() % 2 == 0) {
            GlideApp.with(getActivity()).load(R.drawable.rectangle_black).into(bg);
//            } else {
//                GlideApp.with(getActivity()).load(R.drawable.rectangle_green).into(bg);
//            }
            GlideApp.with(getActivity())
                    .load(item.getLogo())
                    .listener(new RequestListener<Drawable>() {
                        @Override
                        public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                            return false;
                        }

                        @Override
                        public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
//                            if (EmptyUtils.isNotEmpty(resource)) {
//                                CommonUtil. changeSVGColor(getActivity(), helper.getView(R.id.img_bizhongicon), resource, R.color.black);
//                                return true;
//                            }
                            return false;
                        }
                    })
                    .error(R.drawable.icon_arctic)
                    .into((ImageView) helper.getView(R.id.img_bizhongicon));

//            Glide.with(context).load(item.getLogo()).into((ImageView)helper.getView(R.id.img_bizhongicon));
            helper.setText(R.id.name, item.getName());
//            if(MyApp.wallets!=null){
            for (UserWalletData.DataBean.WalletsBean walletsBean : MyApp.wallets) {
                if (walletsBean.getName().equalsIgnoreCase(item.getName())) {
                    helper.setText(R.id.desc, walletsBean.getEngName());
                    helper.setText(R.id.money2, CommonUtil.saveToPoint4WithNo(walletsBean.getPrice()) + "");
                    GlideApp.with(getActivity())
                            .load(walletsBean.getLogo())
                            .listener(new RequestListener<Drawable>() {
                                @Override
                                public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                                    return false;
                                }

                                @Override
                                public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
//                                    if (EmptyUtils.isNotEmpty(resource)) {
//                                        CommonUtil. changeSVGColor(getActivity(), helper.getView(R.id.img_bizhongicon), resource, R.color.black);
//                                        return true;
//                                    }
                                    return false;
                                }
                            })
                            .error(R.drawable.icon_arctic)
                            .into((ImageView) helper.getView(R.id.img_bizhongicon));
                    if (item.getName().equalsIgnoreCase("ETH")) {
                        price = 0.0000d;
                    }
                    price += item.getMoney() * walletsBean.getPrice();
                    EventBus.getDefault().post(new RefreshPriceEvent(-1, price));
                    break;
                }
            }
            helper.setText(R.id.money1, CommonUtil.saveToPoint4WithNo(item.getMoney()) + "");
//            helper.setText(R.id.desc, item.getEngName());

//            helper.setText(R.id.money2, item.getPrice() + "");
            helper.setOnClickListener(R.id.rly_currencyitem, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    TextView name = v.findViewById(R.id.name);
                    if (name.getText().toString().equalsIgnoreCase("ETH")) {
                        TokenModel token = new TokenModel("", "ETH", true);
                        jumpToTokenDetail(token);
                    } else {
                        TokenModel token = new TokenModel(Constant.G12ContractAddress, Constant.G12ContractName, false);
                        jumpToTokenDetail(token);
                    }
                }
            });
        }


        public void jumpToTokenDetail(TokenModel token) {
            Intent i = new Intent(getActivity(), TokenDetailActivity1.class);
            i.putExtra("index", mIndex);
            i.putExtra("token", token);
            startActivity(i);
        }


    }

    UserWalletData.DataBean.UserMsgBean userMsgBean;

    /***  弹出提示公告  ***/
    private void showCodeDialog() {
        CandyDialogFragment dialogFragment = new CandyDialogFragment();
        dialogFragment.setDialogBean(new BaseDialogBean(userMsgBean));

        dialogFragment.setListener(new CandyDialogFragment.ClickListener() {
            @Override
            public void Listener(String name, int potion) {
                startActivity(BillActivity.class);
            }
        });

        dialogFragment.show(getChildFragmentManager(), "NoticeDialogFragment");
        getFragmentManager().executePendingTransactions();
    }
}

